home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / DropFTP / pref.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-03  |  3.6 KB  |  144 lines  |  [TEXT/KAHL]

  1. #include "pref.h"
  2. #include <string.h>
  3. #include "folders.h"    
  4. #include "ftp.h"
  5.     
  6. #define kCreatorType 'dFTP'
  7. #define kFileType 'pref'
  8. #define kUserStrings 1000
  9. #define kHostString 1
  10. #define kUserString 2
  11. #define kPswdString 3
  12.  
  13. extern char host[256];
  14. extern char user[256];
  15. extern char pswd[256];
  16.  
  17. //--------------------------------------------------------------------------
  18.     short OpenPref(short *refNum,short perm)
  19. //--------------------------------------------------------------------------
  20. {
  21.     SysEnvRec theWorld;
  22.     FSSpec myFSSpec;
  23.     short oe,foundVRefNum;
  24.     long foundDirID;
  25.     
  26.     oe = SysEnvirons(1,&theWorld);
  27.     
  28.     oe = FindFolder(theWorld.sysVRefNum,kPreferencesFolderType,
  29.                     false,&foundVRefNum,&foundDirID);
  30.     oe = FSMakeFSSpec(foundVRefNum,foundDirID,"\pDropFTP Pref",&myFSSpec);
  31.     if (oe == fnfErr && perm != fsRdPerm ) {
  32.         oe = FSpCreate(&myFSSpec,kCreatorType,kFileType,smSystemScript);
  33.     } 
  34.     
  35.     if (oe == noErr) {
  36.         oe = FSpOpenDF(&myFSSpec,perm,refNum);
  37.         oe = SetFPos(*refNum,fsFromStart,0);
  38.     } else {
  39.          if (perm != fsRdPerm)
  40.              ErrorMessage("Trouble saving pref file");
  41.     }
  42.     return oe;
  43.     
  44. }
  45.  
  46. //--------------------------------------------------------------------------
  47.     void ReadPref(void)
  48. //--------------------------------------------------------------------------
  49. {
  50.     short len,refNum,oe;
  51.     long count;
  52.     
  53.     if (OpenPref(&refNum,fsRdPerm) == noErr){
  54.         count = 2;    //read number of strings ignore for now
  55.         oe = FSRead(refNum,&count,&len);
  56.         
  57.         count = 2;    //read in str len
  58.         oe = FSRead(refNum,&count,&len);
  59.         count = len;
  60.         oe = FSRead(refNum,&count,host);
  61.         host[len] = 0;
  62.         
  63.         count = 2;    //read in str len
  64.         oe = FSRead(refNum,&count,&len);
  65.         count = len;
  66.         oe = FSRead(refNum,&count,user);
  67.         user[len] = 0;
  68.         
  69.         count = 2;    //read in str len
  70.         oe = FSRead(refNum,&count,&len);
  71.             count = len;
  72.         oe = FSRead(refNum,&count,pswd);
  73.         pswd[len] = 0;
  74.         ScramblePW (pswd,len);        // unscramble
  75.         oe = FSClose(refNum);
  76.     } else {
  77.         Str255 temp;
  78.     
  79.         GetIndString(temp,kUserStrings,kHostString);
  80.         strcpy(host,p2cstr((StringPtr)temp));
  81.         GetIndString(temp,kUserStrings,kUserString);
  82.         strcpy(user,p2cstr((StringPtr)temp));
  83.         GetIndString(temp,kUserStrings,kPswdString);
  84.         strcpy(pswd,p2cstr((StringPtr)temp));
  85.     }
  86. }
  87.  
  88. //--------------------------------------------------------------------------
  89.     void WritePref(void)
  90. //--------------------------------------------------------------------------
  91. {
  92.     short len,refNum,oe;
  93.     long count;
  94.     
  95.     if (OpenPref(&refNum,fsRdWrPerm) ==noErr){
  96.         count = 2;    //read number of strings ignore for now
  97.         len = 3;
  98.         
  99.         oe = FSWrite(refNum,&count,&len);
  100.         
  101.         count = 2;    
  102.         len = strlen(host);
  103.         oe = FSWrite(refNum,&count,&len);
  104.         count = len;
  105.         oe = FSWrite(refNum,&count,host);
  106.  
  107.         
  108.         count = 2;    
  109.         len = strlen(user);
  110.         oe = FSWrite(refNum,&count,&len);
  111.         count = len;
  112.         oe = FSWrite(refNum,&count,user);
  113.     
  114.     
  115.         count = 2;    
  116.         len = strlen(user);
  117.         ScramblePW (pswd,len);        // scramble
  118.         oe = FSWrite(refNum,&count,&len);
  119.         count = len;
  120.         oe = FSWrite(refNum,&count,pswd);
  121.         ScramblePW (pswd,len);        // unscramble
  122.         oe = FSClose(refNum);
  123.     }
  124. }
  125. /*----------------------------------------------------------------------------
  126.     ScramblePW
  127.     
  128.     Scrambles (and unscrambles) saved passwords. This is not really secure,
  129.     just something to foil people browsing using disk editors.
  130.     
  131.     Entry:    pw = the password.
  132.             len = length of password.
  133.             
  134.     Exit:    Each byte nibble-swapped and bit-flipped.
  135. ----------------------------------------------------------------------------*/
  136.  
  137. static void ScramblePW (char *pw, short len)
  138. {
  139.     char *p, *pEnd;
  140.     
  141.     pEnd = pw + len;
  142.     for (p = pw; p < pEnd; p++) *p = (((*p >> 4) & 0x0f) | ((*p & 0x0f) << 4)) ^ 0xff;
  143. }
  144.